<template>
	<!-- 倒计时组件 -->
	<view class="countdown-container">
		<!-- 状态标题 -->
		<view class="countdown-status" v-if="status === 'upcoming'">
			距离开始还有
		</view>
		<view class="countdown-status" v-if="status === 'ongoing'">
			剩余时间
		</view>
		<view class="countdown-status" v-if="status === 'ended'">
			已结束
		</view>

		<!-- 倒计时数字显示 -->
		<view class="countdown-display" v-if="status !== 'ended'">
			<view class="time-item">
				<text class="time-number">{{ days }}</text>
				<text class="time-label">天</text>
			</view>
			<text class="separator">:</text>
			<view class="time-item">
				<text class="time-number">{{ hours }}</text>
				<text class="time-label">时</text>
			</view>
			<text class="separator">:</text>
			<view class="time-item">
				<text class="time-number">{{ minutes }}</text>
				<text class="time-label">分</text>
			</view>
			<text class="separator">:</text>
			<view class="time-item">
				<text class="time-number">{{ seconds }}</text>
				<text class="time-label">秒</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			// 开始时间戳(毫秒)
			startTime: {
				type: Number,
				required: true,
				validator: (value) => {
					return typeof value === 'number' && value > 0;
				}
			},
			// 结束时间戳(毫秒)
			endTime: {
				type: Number,
				required: true,
				validator: (value) => {
					return typeof value === 'number' && value > 0;
				}
			}
		},
		data() {
			return {
				days: '00',
				hours: '00',
				minutes: '00',
				seconds: '00',
				status: 'upcoming', // upcoming: 未开始, ongoing: 进行中, ended: 已结束
				timer: null
			};
		},
		watch: {
			// 监听时间戳变化，重新初始化倒计时
			startTime() {
				this.clearTimer();
				this.initCountdown();
			},
			endTime() {
				this.clearTimer();
				this.initCountdown();
			}
		},
		mounted() {
			// 初始化倒计时
			this.initCountdown();
		},
		beforeDestroy() {
			// 组件销毁前清除定时器
			this.clearTimer();
		},
		methods: {
			// 初始化倒计时
			initCountdown() {
				this.updateCountdown();
				this.timer = setInterval(() => {
					this.updateCountdown();
				}, 1000);
			},

			// 清除定时器
			clearTimer() {
				if (this.timer) {
					clearInterval(this.timer);
					this.timer = null;
				}
			},

			// 更新倒计时
			updateCountdown() {
				const now = Date.now();
				let remainingTime;

				// 判断当前状态
				if (now < this.startTime) {
					// 未开始，计算距离开始的时间
					this.status = 'upcoming';
					remainingTime = this.startTime - now;
				} else if (now < this.endTime) {
					// 进行中，计算距离结束的时间
					this.status = 'ongoing';
					remainingTime = this.endTime - now;
				} else {
					// 已结束
					this.status = 'ended';
					this.clearTimer();
					this.$emit('countdownEnd'); // 触发结束事件
					return;
				}

				// 转换时间格式
				const totalSeconds = Math.floor(remainingTime / 1000);
				this.days = this.formatNumber(Math.floor(totalSeconds / (60 * 60 * 24)));
				this.hours = this.formatNumber(Math.floor((totalSeconds % (60 * 60 * 24)) / (60 * 60)));
				this.minutes = this.formatNumber(Math.floor((totalSeconds % (60 * 60)) / 60));
				this.seconds = this.formatNumber(totalSeconds % 60);
			},

			// 格式化数字为两位数
			formatNumber(num) {
				return num < 10 ? `0${num}` : num.toString();
			}
		}
	};
</script>

<style scoped>
	.countdown-container {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 16rpx;
	}

	.countdown-status {
		font-size: 28rpx;
		color: #333;
		margin-bottom: 16rpx;
	}

	.countdown-display {
		display: flex;
		align-items: center;
	}

	.time-item {
		display: flex;
		flex-direction: column;
		align-items: center;
		margin: 0 8rpx;
	}

	.time-number {
		font-size: 48rpx;
		font-weight: bold;
		color: #333;
		background-color: #f5f5f5;
		padding: 12rpx 20rpx;
		border-radius: 8rpx;
		min-width: 80rpx;
		text-align: center;
	}

	.time-label {
		font-size: 24rpx;
		color: #666;
		margin-top: 8rpx;
	}

	.separator {
		font-size: 48rpx;
		font-weight: bold;
		color: #333;
		margin: 0 4rpx;
	}
</style>